<template>
  <el-dialog v-model="dialogVisible" title="授课老师" width="80%" append-to-body :before-close="handleClose">
    <el-form :inline="true" :model="formInline" class="grids gridColumns5">
      <el-form-item label="授课方向">
        <el-select v-model="formInline.teachingDirection" default-first-option placeholder="请选择" clearable>
          <el-option v-for="(item,index) in teachingDirectionOptions" :key="index" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="授课类型">
        <el-select v-model="formInline.teachingType" placeholder="请选择" clearable>
          <el-option v-for="(item,index) in teachingTypeOptions" :key="index" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="授课项目">
        <el-select v-model="formInline.teachingProgram" placeholder="请选择" clearable>
          <el-option v-for="(item,index) in teachingProgramOptions" :key="index" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item label="课时">
        <el-input v-model="formInline.classHour" placeholder="课时" clearable />
      </el-form-item>
      <el-form-item label="讲师姓名">
        <el-input v-model="formInline.lecturerName" placeholder="讲师姓名" clearable />
      </el-form-item>
      <el-form-item label="联系电话">
        <el-input v-model="formInline.mobile" placeholder="联系电话" clearable />
      </el-form-item>
      <el-form-item label="课酬(天)">
        <el-input v-model="formInline.lessonRemuneration" placeholder="课酬（天）" type="number" clearable />
      </el-form-item>
      <el-form-item label="常住地">
        <el-select v-model="formInline.permanentResidence" placeholder="请选择" clearable>
          <el-option v-for="(item,index) in permanentResidenceOptions" :key="index" :label="item" :value="item" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
        <el-button @click="init">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :data="tableData.rows" size="small" maxHeight="60vh" @row-click="rowClick">
      <el-table-column type="index" width="50" fixed="left" />
      <el-table-column prop="teachingDirection" label="授课方向" fixed="left">
        <template #default="scope">
          <div style="color:#409EFF">{{scope.row.teachingDirection}}</div>
        </template>
      </el-table-column>
      <el-table-column prop="teachingType" label="授课类型" width="100" fixed="left">
        <template #default="scope">
          <div style="color:#409EFF">{{scope.row.teachingType}}</div>
        </template>
      </el-table-column>
      <el-table-column prop="teachingProgram" label="授课项目" :width="teachingProgramWidth" />
      <el-table-column prop="classHour" label="课时" width="60" />
      <el-table-column label="讲师姓名">
        <template #default="scope">
          <div style="color:#C95C19;cursor:pointer" @click="handleEdit(scope.row,1)">{{scope.row.lecturerName}}</div>
        </template>
      </el-table-column>
      <el-table-column prop="position" label="职位" width="250" />
      <el-table-column prop="lessonRemuneration" label="课酬(天)" width="100" />
      <el-table-column prop="remarks" label="备注" width="150" show-overflow-tooltip />
      <el-table-column prop="permanentResidence" label="常住地" width="100" />
      <el-table-column prop="introduce" label="讲师简介" min-width="200" show-overflow-tooltip />
    </el-table>

    <Pagination v-if="tableData['total']" :total="tableData['total']" v-model:page="formInline.pageIndex" v-model:limit="formInline.pageSize" @pagination="getInfo" />

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">
          确 认
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
const { proxy } = getCurrentInstance();
const emit = defineEmits(["update:userName", "update:userId", "fatherMethods"]);

let formInline = ref({
  pageIndex: 1,
  pageSize: 50,
  teachingDirection: "",
  teachingType: "",
  teachingProgram: "",
  classHour: "",
  lecturerName: "",
  lessonRemuneration: "",
  permanentResidence: "",
});
let tableData = ref({});
let teachingProgramWidth = ref(0);
let teachingDirectionOptions = ref([]);
let teachingTypeOptions = ref([]);
let teachingProgramOptions = ref([]);
let permanentResidenceOptions = ref([]);
const dialogVisible = ref(false);

async function open() {
  await getInfo();
  dialogVisible.value = true;
}

function rowClick(e) {
  emit("update:userName", e.lecturerName);
  emit("update:userId", e._id);
  emit("fatherMethods", e);
  handleClose();
}

function handleClose() {
  formInline.value = {
    pageIndex: 1,
    pageSize: 50,
    teachingDirection: "",
    teachingType: "",
    teachingProgram: "",
    classHour: "",
    lecturerName: "",
    lessonRemuneration: "",
    permanentResidence: "",
  };
  dialogVisible.value = false;
}

function onSubmit() {
  formInline.value.pageIndex = 1;
  getInfo();
}

function init() {
  formInline.value = {
    pageIndex: 1,
    pageSize: 50,
    teachingDirection: "",
    teachingType: "",
    teachingProgram: "",
    classHour: "",
    lecturerName: "",
    lessonRemuneration: "",
    permanentResidence: "",
  };
  getInfo();
}

async function getInfo() {
  tableData.value = await proxy.$apis.teachers({
    type: "list",
    data: formInline.value,
  });
  console.log("tableData", tableData.value);

  let resp = await proxy.$apis.teachers({
    type: "longest",
  });
  if (resp.data && resp.data.length) {
    teachingProgramWidth.value = resp.data[0].length * 12.5 + 30;
  }
  getOptions();
}

async function getOptions() {
  let array = [
    "teachingDirection",
    "teachingType",
    "teachingProgram",
    "permanentResidence",
  ];
  for (let index = 0; index < array.length; index++) {
    const element = array[index];
    switch (element) {
      case "teachingDirection":
        teachingDirectionOptions.value = await proxy.$apis.teachers({
          type: "options",
          data: { key: "teachingDirection" },
        });
        break;
      case "teachingType":
        teachingTypeOptions.value = await proxy.$apis.teachers({
          type: "options",
          data: { key: "teachingType" },
        });
        break;
      case "teachingProgram":
        teachingProgramOptions.value = await proxy.$apis.teachers({
          type: "options",
          data: { key: "teachingProgram" },
        });
        break;
      case "permanentResidence":
        permanentResidenceOptions.value = await proxy.$apis.teachers({
          type: "options",
          data: { key: "permanentResidence" },
        });
        break;
      default:
        break;
    }
  }
}

watch(
  () => [
    formInline.value.teachingDirection,
    formInline.value.teachingType,
    formInline.value.teachingProgram,
    formInline.value.classHour,
    formInline.value.mobile,
    formInline.value.mobile,
    formInline.value.lecturerName,
    formInline.value.permanentResidence,
    formInline.value.lessonRemuneration,
  ],
  (newValue, oldValue) => {
    if (newValue) onSubmit();
  },
  { deep: true, immediate: true }
);

defineExpose({ open });
</script>



<style lang="scss" scoped>
</style>

